﻿using Castle.DynamicProxy;
using Microsoft.AspNetCore.Http;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyExtension.AOP
{
    public class LogAop : IInterceptor
    {
        private readonly IHttpContextAccessor _accessor;

        public LogAop(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }
        public void Intercept(IInvocation invocation)
        {
           string userName  = _accessor.HttpContext?.User?.Identity?.Name;
            try
            {
                Log.Logger.Information($"当前登录用户：{userName}，执行方法前");
                invocation.Proceed();
                Log.Logger.Information($"执行方法后");
            }
            catch (Exception ex)
            {
                Log.Logger.Information($"执行方法：{invocation.Method.Name}，异常了，{ex.Message}");
                throw;
            }
        }
    }
}
